<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * 客户信息db管理
 */

require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

class CustomerCom{
    
    function __construct()
    {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql ($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }
    
    function addCustomer($userdata,$openid){
        try{
            $sql = "insert into customer(nickname,headimg,openid) values('{$userdata['nickName']}','{$userdata['avatarUrl']}','{$openid}');SELECT @@Identity";
            $ret = $this->mysql->query($sql);
            return $ret[0][0]['@@Identity'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function checkOpenid($openid){
        try{
            $sql = "select id from customer where openid='{$openid}'";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0][0]['id'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getCustomerById($id){
        try{
            $sql = "select * from customer where id={$id}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getCustomerlist($minlimit, $limitnum){
        try{
            $sql = "select * from customer limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllNum(){
        try{
            $sql = "select count(1) from customer";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
}